<!--
@name:dialogTarget
@author:Zhenglei
@date:16:06
@description:dialogTarget
@update:2022/8/816:06
-->
<template>
  <el-dialog :title="title"
             :visible.sync="visible"
             :before-close="modalClose"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="50%">
    <el-form :model="subData"
             label-position="right"
             label-width="150px"
             ref="formModelRef"
             :rules='formModelRules'>
      <el-form-item label="指标：" prop="relationId">
        <el-select v-model="subData.relationId"
                   filterable
                   class="w-300"
                   :disabled="isEdit"
                   @change="handleChangeIndex"
                   placeholder="请选择指标">
          <el-option v-for="item in targetDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="结论：">
        <el-select v-model="subData.relationName"
                   filterable
                   class="w-300"
                   clearable
                   :disabled="isEdit"
                   placeholder="请选择结论">
          <el-option v-for="item in conclusionDataSource"
                     :key="item"
                     :label="item"
                     :value="item">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="健康建议：">
        <vue-neditor-wrap :destroy="true"
                          v-model="subData.suggest"
                          :config="myConfig"></vue-neditor-wrap>
      </el-form-item>


    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleSave()">保 存
      </el-button>
    </p>

  </el-dialog>
</template>

<script>
import VueNeditorWrap from "vue-neditor-wrap";

export default {
  name: "dialogTarget",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      myConfig: {
        // 你的UEditor资源存放的路径,相对于打包后的index.html
        UEDITOR_HOME_URL: this.api.uEditorHomeUrl,
        // 编辑器不自动被内容撑高
        autoHeightEnabled: false,
        // 初始容器高度
        initialFrameHeight: '418',
        // 初始容器宽度
        initialFrameWidth: '100%',
        // 关闭自动保存
        enableAutoSave: false,
        scaleEnabled: true
      },
      title: '新 增',
      subData: {
        id: null,
        relationId: '',
        relationName: '',
        suggest: '',
        type: 50,
      },
      isEdit: false,
      targetDataSource: [],
      conclusionDataSource: [],
      statusDataSource: [
        {
          id: 1,
          name: '低风险'
        }, {
          id: 2,
          name: '中风险'
        }, {
          id: 3,
          name: '高风险'
        },
      ],
      formModelRules: {
        relationId: [{required: true, message: '请选择健康地图病种', trigger: 'blur'},],
      }

    }
  },
  created() {
    if (this.dataObj.id) {
      this.title = '编 辑'
      this.isEdit = true
      this.subData.id = this.dataObj.id
      this.subData.relationId = this.dataObj.relationId
      this.subData.relationName = this.dataObj.relationName
      this.subData.suggest = this.dataObj.suggest


    } else {
      this.isEdit = false
      this.title = '新 增'
    }
    this.getSuggestIndexItemList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false);
    },
    //选择指标
    handleChangeIndex(val) {
      this.subData.relationName = ''
      this.conclusionDataSource = this.targetDataSource.find(item => item.id === val).results
    },
    handleSave() {
      this.$refs.formModelRef.validate(valid => {
        if (valid) {
          if (this.subData.relationName) {
            this.subData.type = 51
          } else {
            this.subData.type = 50
          }
          if (this.isEdit) {
            //编辑
            this.post(this.api.healthMap.updateSuggest, this.subData).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('更新成功!');
                this.$parent.getList();
                this.modalClose();
              }
            })
          } else {
            //新增
            let arr = []
            arr.push(this.subData)
            this.post(this.api.healthMap.addSuggest, {
              jsonData: arr,
              searchType: 5
            }).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('保存成功!');
                this.$parent.getList();
                this.modalClose();
              }
            })
          }

        }
      })
    },
    //查询指标
    getSuggestIndexItemList() {
      this.post(this.api.healthMap.getSuggestIndexItemList).then((res) => {
        if (res.data.code === '200') {
          this.targetDataSource = res.data.object
          if (this.dataObj.id) {
            this.conclusionDataSource = this.targetDataSource.find(item => item.id === this.subData.relationId).results
          }
        }
      })
    },
  },
  components: {
    VueNeditorWrap
  }
}
</script>

<style scoped>

</style>
